package com.example.hal.model;
 
import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
 
public class DepartementXMLHandler extends DefaultHandler {
 
	Boolean currentElement = false;
	String currentValue = null;
	public static Departement departement = null;
	public static List<Departement> departements=new ArrayList<Departement>();
 
	
 
	public DepartementXMLHandler() {
		super();
	}

	public static Departement getDepartement() {
		return departement;
	}

	public static void setDepartement(Departement departement) {
		DepartementXMLHandler.departement = departement;
	}
	

	/** Called when tag starts ( ex:- <name>AndroidPeople</name> 
	 * -- <name> )*/
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
 
		currentElement = true;
		if(localName.equals("departements"))
		{
			departements=new ArrayList<Departement>();
		}
		else if (localName.equals("departement"))
		{
			/** Start */ 
			departement = new Departement();
		} else if (localName.equals("nom")) {
			/** Get attribute value */
			String attr = attributes.getValue("nom");
			departement.setNom(attr);
		}
 
	}
 
	/** Called when tag closing ( ex:- <name>AndroidPeople</name> 
	 * -- </name> )*/
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
 
		currentElement = false;
 
		/** set value */ 
		if (localName.equalsIgnoreCase("nom"))
			departement.setNom(currentValue);
		else if (localName.equalsIgnoreCase("code"))
			departement.setCodePostal(currentValue);
		else if(localName.equalsIgnoreCase("departement"))
			departements.add(departement);
 
	}
 
	/** Called to get tag characters ( ex:- <name>AndroidPeople</name> 
	 * -- to get AndroidPeople Character ) */
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
 
		if (currentElement) {
			currentValue = new String(ch, start, length);
			currentElement = false;
		}
 
	}

	public List<Departement> getData() {
		
		return departements;
	}
 
}